<template>
  <div class="register-container">
    <el-form ref="dataForm" :model="dataModel" :rules="rules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h5 class="title">新 用 户 注 册</h5>
      </div>

      <el-form-item prop="userCode">
        <el-input
          v-model="dataModel.userCode"
          type="text"
          placeholder="登录名"
          size="small"
        />
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          v-model="dataModel.password"
          type="password"
          placeholder="密码"
          size="small"
        />
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          v-model="confirmPassword"
          type="password"
          placeholder="确认密码"
          size="small"
        />
      </el-form-item>

      <el-form-item prop="userName">
        <el-input
          v-model="dataModel.userName"
          type="text"
          placeholder="姓名"
          size="small"
        />
      </el-form-item>

      <el-form-item prop="mobile">
        <el-input
          v-model="dataModel.mobile"
          type="tel"
          placeholder="手机号"
          size="small"
        />
      </el-form-item>

      <el-form-item prop="email">
        <el-input
          v-model="dataModel.email"
          type="email"
          placeholder="邮箱"
          size="small"
        />
      </el-form-item>

      <el-form-item prop="captchaCode">
        <el-input
          v-model="dataModel.captchaCode"
          type="text"
          placeholder="验证码"
          size="small"
        />
        <el-image v-if="verifyCodeImg !== undefined && verifyCodeImg !== null" class="verify-img" style="width: 120px; height: 36px" :src="verifyCodeImg" @click="refreshVerifyCode()" />
      </el-form-item>

      <el-button :loading="loading" type="info" native-type="submit" style="width:100%;" @click.native.prevent="handleRegister">注册</el-button>

      <div class="navs">
        <router-link to="/login">
          <el-button type="text"><i class="el-icon-back" /> 返回登录</el-button>
        </router-link>
      </div>

    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { register, getCaptcha } from '@/api/auth'

export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不得少于6个字符'))
      } else {
        callback()
      }
    }
    const validateCaptcha = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else {
        callback()
      }
    }
    return {
      dataModel: {
        userCode: '',
        userName: '',
        password: '',
        mobile: '',
        email: '',
        type: 1,
        captchaId: null,
        captchaCode: ''
      },
      rules: {
        userCode: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        captchaCode: [{ required: true, trigger: 'blur', validator: validateCaptcha }]
      },
      confirmPassword: '',
      verifyCodeImg: null,
      loading: false
    }
  },
  created() {
    this.refreshVerifyCode()
  },
  methods: {
    refreshVerifyCode() {
      getCaptcha({ type: 0 }).then((response) => {
        const result = response.data
        this.dataModel.captchaId = result.captchaId
        this.verifyCodeImg = result.src
      }).catch((err) => {
        console.log(err)
      })
    },
    handleRegister() {
      if (this.confirmPassword !== this.dataModel.password) {
        this.$message.error('确认密码与密码输入的不一致')
        return
      }
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.loading = true
          register(this.dataModel).then((response) => {
            console.log(response)
            this.$router.push({ path: '/reg-redirect' })
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$light_gray:#aaa;
$cursor: #bbb;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .register-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.register-container {
  .el-input {
    display: inline-block;
    height: 36px;
    width: 82%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$dark_gray:#889aa4;
$light_gray:#eee;

.register-container {
  position: relative;
  margin: 60px auto;
  width: 400px;

  .navs {
    font-size: 14px;

    button {
      color: #889aa4;
    }
  }

  .tips {
    margin: 120px 0 10px 0;
    font-size: 14px;
    color: #ddd;
    text-align: center;

    * {
      margin-right: 8px;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 40px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 24px;
      color: $light_gray;
      margin: 0px auto 20px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .verify-img {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }

}
</style>
